package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarSimple;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarPatronsData;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarUserData;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.enums.eUnionWarWinMark;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.logic.sort.UnionWarPatronsSort;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_UNION_WAR_CHANGE_DISPATCH_SEQUENCE, desc = "调整出战顺序")
public class CrossUnionWarChangeDispatchSequenceCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        long pid = packet.getPlayerId();
        UnionActivityGroupProto.BaseUnionActivityGroupMsg baseMsg = UnionActivityGroupProto.BaseUnionActivityGroupMsg.parseFrom(packet.getBytes());
        packet = YanQuMessageUtils.buildMessageByte(baseMsg.getCode(), baseMsg.getMsg().toByteArray());
        packet.setPlayerId(pid);

        CrossUnionWarProto.CrossUnionWarChangeDispatchSequenceReqMsg reqMsg = CrossUnionWarProto.CrossUnionWarChangeDispatchSequenceReqMsg.parseFrom(packet.getBytes());

        UnionActivityGroupUnion unionData = Cross2UnionActivityGroupMgr.getUnionData(reqMsg.getActivityId(), packet.getPlayerId());

        //先判断权限
        if(unionData != null){
            CrossUnionWarSimple unionWarSimple = CrossUnionWarMgr.getCrossUnionWarSimple(unionData.getActivityId(), unionData.getUnionUid());
            UnionActivityGroupUser groupUser = unionData.getUserMap().get(packet.getPlayerId());
            ActivityInfo activityInfo = CrossUnionWarActivityMgr.getOpenActivity(unionData.getActivityId());
            if(groupUser != null && unionWarSimple != null && activityInfo != null){
                if(groupUser.getPosition() == eUnionPosition.Master.getValue() || unionWarSimple.getCommander() == packet.getPlayerId()){
                    CrossUnionWarDetail unionWarDetail = CrossUnionWarMgr.getCrossUnionWarDetailReadOnly(reqMsg.getActivityId(), reqMsg.getPhase(),
                            reqMsg.getSession(), unionWarSimple.getUnionUid());
                    if(unionWarDetail != null){
                        // 已经出结果的不再更新
                        if(eUnionWarWinMark.Default.getValue() != unionWarDetail.getWinMark()){
                            return;
                        }
                        synchronized (unionWarDetail){
                            long startAdd = 100;
                            long beginTime = 0;
                            for (Long uid : reqMsg.getUserIdList()) {
                                UnionWarUserData userData = unionWarDetail.getUserDataMap().get(uid);
                                if(userData.getModifyTime() > beginTime){
                                    beginTime = userData.getModifyTime();
                                }
                            }
                            for (Long uid : reqMsg.getUserIdList()) {
                                UnionWarUserData userData = unionWarDetail.getUserDataMap().get(uid);
                                userData.setModifyTime(beginTime + startAdd);
                                startAdd += 100;
                            }
                            for (int i = 0; i < unionWarDetail.getPatronsDataList().size(); i++) {
                                UnionWarPatronsData patronsData = unionWarDetail.getPatronsDataList().get(i);
                                if(patronsData != null){
                                    UnionWarUserData userData = unionWarDetail.getUserDataMap().get(patronsData.getUserId());
                                    patronsData.setModifyTime(userData.getModifyTime());
                                }
                            }

                            unionWarDetail.getPatronsDataList().sort(new UnionWarPatronsSort());
                            unionWarDetail.setUpdateOption();
                        }

                        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_CHANGE_DISPATCH_SEQUENCE,
                                CrossUnionWarProto.CrossUnionWarChangeDispatchSequenceRespMsg.newBuilder().setRet(0)));
                    }
                }
            }
        }
    }

    @Override
    public void sendData() {

    }
}
